package dbAccess;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import model.ConnectionSettings;

public class SettingsSaver {
	private static final String PATH = "c:"+File.separator+"sylset.conf";
	public boolean saveSettings(ConnectionSettings connSet){
	    try{
	    	BufferedWriter writer = new BufferedWriter(new FileWriter(SettingsSaver.PATH));
	    	writer.write(connSet.getDbAdress()+";"+connSet.getDbPort()+";"+connSet.getDbLogin()+";"+connSet.getDbPass());
	    	writer.close();
	    	return true;
	    }
	    catch (Exception e){
	    	return false;
	    }
	}
	public ConnectionSettings loadSettings() {
		ConnectionSettings connSet = null;
		try{
			File setFile = new File(SettingsSaver.PATH);
			if(!setFile.exists()){
				setFile.createNewFile();
			}else{
				BufferedReader reader = new BufferedReader(new FileReader(SettingsSaver.PATH));
			    String line = reader.readLine();
			    while (line!=null){
			    	String[] values = line.split(";");
			    	if ( values.length == 4 ){
			        	connSet = new ConnectionSettings(values[0], values[1], values[2], values[3]);
			        }
			    	line = reader.readLine();
			    }
			}
		}catch (Exception e) {

		}
		return connSet;
	}
}
